package com.mingyuan.demo1;

import com.mingyuan.PremainAgent;
import com.mingyuan.demo2.NacosRegister;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.utility.JavaModule;

import static net.bytebuddy.matcher.ElementMatchers.named;

public class ByteBuddyAgentTransformer implements AgentBuilder.Transformer {
    @Override
    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
        if (typeDescription.getPackage().getActualName().equals(PremainAgent.scanPackage)
        ) {
            String targetClassName = typeDescription.getSimpleName();
            System.out.println("----------------------- target class:" + targetClassName);
            // 委托
            return builder.method(named(PremainAgent.sayHelloMethod)).intercept(MethodDelegation.to(MethodCostTime.class))
                    .method(named(PremainAgent.nacosStartMethod)).intercept(MethodDelegation.to(NacosRegister.class));
        }
        return builder;
    }
}